package com.jic.interaction.payOrder.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jic.interaction.common.enums.RedisKey;
import com.jic.interaction.common.utils.HttpClientUtil;
import com.jic.interaction.common.utils.RedisService;
import com.jic.interaction.payOrder.service.PayOrderService;
import com.jic.interaction.sign.SingUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
@Slf4j
public class PayOrderServiceImpl implements PayOrderService {
    @Autowired
    private RedisService redisService;
    //预下单地址
    @Value("${pay.preMakeOrderUrl}")
    private String preMakeOrderUrl;
    //预下单回查地址
    @Value("${pay.preMakeOrderCheckUrl}")
    private String preMakeOrderCheckUrl;
    //退款地址
    @Value("${pay.refundOrderUrl}")
    private String refundOrderUrl;
    //退款查询地址
    @Value("${pay.refundOrderCheckUrl}")
    private String refundOrderCheckUrl;

    //卡密因子
    @Value("${pay.getPwdFactorUrl}")
    private String getPwdFactorUrl;
    //支付地址
    @Value("${pay.makeOrderUrl}")
    private String makeOrderUrl;
    //支付地址回查
    @Value("${pay.makeOrderCheckUrl}")
    private String makeOrderCheckUrl;
    //微信支付
    @Value("${pay.wxPayUrl}")
    private String wxPayUrl;
    //微信支付回调地址
    @Value("${pay.wxCallBackPay}")
    private String wxCallBackPay;


    @Value("${wz.wzCash.appid}")
    private String appid;

    @Value("${wz.wzCash.secret}")
    private String secret;

    @Value("${wz.wzCash.version}")
    private String version;

    @Value("${pay.notifyUrlRmb}")
    private String notifyUrlRmb;
    /**
     * 地址需要经过本地nginx转发
     */
    @Value("${wz.wzCash.url}")
    private String url;

    @Override
    public JSONObject prePayOrder(JSONObject jsonObject) {
        if(jsonObject==null) {
            return null;
        }
        log.debug("==========【请求参数】==========",jsonObject.toJSONString());
        String body = "";
        try {
            body = HttpClientUtil.sendJsonPost(getSignUrl(jsonObject.toJSONString(),preMakeOrderUrl),jsonObject.toJSONString());
            jsonObject.clear();
            jsonObject = JSONObject.parseObject(body);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return jsonObject;
    }

    /**
     * @param body(请求参数)
     * @return
     */
    private String getSignUrl(String body,String reUrl){
        String signTicket = redisService.getValue(RedisKey.WZ_CASH_TICKET);
        if(StringUtils.isEmpty(signTicket)){
            log.error("ticket为空!...");
            return "";
        }
        Long timestamp = System.currentTimeMillis();
        String uuid = UUID.randomUUID().toString().replaceAll("-","")
                .substring(0,19);
        String nonce = timestamp + uuid;
        List<String> values = new ArrayList<>();
        values.add(appid);
        values.add(nonce);
        values.add(body);
        values.add(version);
        values.add(signTicket);
        StringBuilder url = new StringBuilder();
        url.append(reUrl+"?app_id="+appid);
        url.append("&nonce="+nonce);
        url.append("&version="+version);
        url.append("&sign="+SingUtils.sign(values));
        return url.toString();
    }

    public static void main(String[] args) {
        List<String> values = new ArrayList<>();
        values.add("b");
        values.add("12");
        values.add("A");
        Collections.sort(values);
        System.out.println(JSONObject.toJSONString(values));
    }
    @Override
    public JSONObject prePayOrderCheck(JSONObject jsonObject) {
        return null;
    }

    @Override
    public JSONObject getPassWord(JSONObject jsonObject) {

        if(jsonObject==null) {
            return null;
        }
        log.debug("==========【请求参数】=========={}",jsonObject);
        String body = null;
        try{
            body = HttpClientUtil.sendJsonPost(getSignUrl(jsonObject.toJSONString(),getPwdFactorUrl),jsonObject.toJSONString());
            jsonObject.clear();
            jsonObject = JSONObject.parseObject(body);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return jsonObject;
    }

    @Override
    public JSONObject wzPay(JSONObject jsonObject) {
        if(jsonObject==null) {
            return null;
        }
        jsonObject.put("notifyUrlRmb",notifyUrlRmb);
        log.debug("==========【请求参数】=========={}",jsonObject);
        String body = "";
        try {
            body = HttpClientUtil.sendJsonPost(getSignUrl(jsonObject.toJSONString(),makeOrderUrl),jsonObject.toJSONString());
            jsonObject.clear();
            log.info("微众支付{}",body);
            jsonObject = JSONObject.parseObject(body);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return jsonObject;
    }

    @Override
    public JSONObject wzPayCheck(JSONObject jsonObject) {
        if(jsonObject==null) {
            return null;
        }
        log.debug("==========【请求参数】=========={}",jsonObject);
        String body = "";
        try {
            body = HttpClientUtil.sendJsonPost(getSignUrl(jsonObject.toJSONString(),makeOrderCheckUrl),jsonObject.toJSONString());
            jsonObject.clear();
            jsonObject = JSONObject.parseObject(body);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return jsonObject;
    }


    /**
     * 退款
     * @param jsonObject
     * @return
     */
    @Override
    public JSONObject refundOrder(JSONObject jsonObject) {
        if(jsonObject==null) {
            return null;
        }
        log.debug("==========【请求参数】=========={}",jsonObject);
        String body = "";
        try {
            body = HttpClientUtil.sendJsonPost(getSignUrl(jsonObject.toJSONString(),refundOrderUrl),jsonObject.toJSONString());
            jsonObject.clear();
            jsonObject = JSONObject.parseObject(body);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return jsonObject;
    }

    /**
     * 退款查询
     * @param jsonObject
     * @return
     */
    @Override
    public JSONObject refundOrderCheck(JSONObject jsonObject) {
        if(jsonObject==null) {
            return null;
        }
        log.debug("==========【请求参数】=========={}",jsonObject.toJSONString());
        String body = "";
        try {
            body = HttpClientUtil.sendJsonPost(getSignUrl(jsonObject.toJSONString(),refundOrderCheckUrl),jsonObject.toJSONString());
            jsonObject.clear();
            jsonObject = JSONObject.parseObject(body);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return jsonObject;
    }

    @Override
    public JSONObject wxPay(JSONObject jsObject) {
        return null;
    }
}
